package com.lz.yz.member.service.impl;

import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.base.util.Constant;
import com.lz.yz.core.AbstractService;
import com.lz.yz.member.dao.WithdrawsMapper;
import com.lz.yz.member.entity.PropertyInfo;
import com.lz.yz.member.entity.Withdraws;
import com.lz.yz.member.model.WithdrawModel;
import com.lz.yz.member.service.PropertyInfoService;
import com.lz.yz.member.service.WithdrawsService;
import com.lz.yz.order.enumeration.TurnoversType;
import com.lz.yz.order.service.TurnoversService;
import com.lz.yz.outing.service.CreateNo;
import com.lz.yz.outing.service.SyncLockService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;


/**
 * Created by CodeGenerator on 2017/11/16.
 */
@Service
@Transactional
public class WithdrawsServiceImpl extends AbstractService<Withdraws> implements WithdrawsService {
    @Resource
    private WithdrawsMapper withdrawsMapper;

    @Autowired
    CreateNo createNo;

    @Autowired
    SyncLockService syncLockService;

    @Autowired
    PropertyInfoService propertyInfoService;

    @Autowired
    TurnoversService turnoversService;


    @Override
    public ReturnValue withdraw(Integer accountId, WithdrawModel withdrawModel) throws IllegalAccessException, NoSuchFieldException, InstantiationException {
        //控制双重点击事件
        if (!syncLockService.syncLock("withdraw/" + accountId, 1, TimeUnit.SECONDS)) {
            return ReturnValue.generateFalseReturnValue("请不要重复提交!");
        }


        Withdraws withdraw = new Withdraws();
        BeanUtils.copyProperties(withdrawModel,withdraw);
        withdraw.setAccountId(accountId);
        withdraw.setAmount(BigDecimal.valueOf(withdrawModel.getAmount().doubleValue()));

        //验证资产的是否大于要提现的金额
        PropertyInfo propertyInfo = propertyInfoService.findBy("accountId",accountId);

        if ((propertyInfo.getBalance()).compareTo(withdrawModel.getAmount()) < 0) {
            return ReturnValue.generateFalseReturnValue("账户余额不足");
        }
        //减去账户余额
        int ret = propertyInfoService.decreaseBalance(accountId, withdrawModel.getAmount());
        if (ret <= 0) {
            return ReturnValue.generateFalseReturnValue("账户扣款失败");
        }

        String turnoverNo = createNo.getNo(accountId, TurnoversType.WITHDRAW.getStr());
        withdraw.setWithdrawNo(turnoverNo);
        int count = withdrawsMapper.insert(withdraw);
        //成功
        if (count <= 0) {
            return ReturnValue.generateFalseReturnValue("申请提现失败");
        } else {
            turnoversService.insert(accountId, TurnoversType.WITHDRAW, Constant.SUBTRACTION,withdrawModel.getAmount(),turnoverNo);
            return ReturnValue.generateTrueReturnValue("提现申请提交成功");
        }
    }
}
